package callback;

/**
 * @Author wsyjiamian@163.com
 * @Description 通过匿名内部类实现回调
 * @Date 2020/8/18 10:56
 */

public class InnerClassCallback {

    private Integer userId;
    private String uName;
    public InnerClassCallback(Integer userId){
        this.userId = userId;
    }

    //触发事件，并将返回的结果更新到外部类
   /* public void clientEvent(){
        Worker wk = new Worker();
        //实现Event接口的内部类
        wk.worker(userId , new Event() {
            public void clickEvent(String userName) {
                uName = userName;
                System.out.println("查询结果：" + uName);
            };
        });
    }*/


    //使用lamda表达式
    public void clientEvent(){
        Worker wk = new Worker();
        wk.worker(userId , (c)-> {this.uName=c ; System.out.println("lamda: "+c);});
    }


    public static void main(String[] args) {
        new InnerClassCallback(1).clientEvent();
    }
}

//在workder执行实际的操作
class Worker{
    public void worker(Integer userId , Event event){
        if(userId == 1){
            event.clickEvent("zs");
        }
    }
}

//匿名内部类需要实现的接口
interface Event{
    void clickEvent(String name);
}









